package server

import (
	"context"
	pb "wire/api/city"
)

type CityGRPCServer struct {
	pb.UnimplementedCityServer
}

func NewCityGRPCServer() *CityGRPCServer {
	return &CityGRPCServer{}
}

// Get
func (s *CityGRPCServer) Get(ctx context.Context, req *pb.GetRequest) (*pb.GetReply, error) {
	return &pb.GetReply{
		Info: &pb.CityInfo{
			Id:     102,
			Code:   "sh",
			Name:   "上海",
			Status: 1,
		},
	}, nil
}

// All
func (s *CityGRPCServer) All(ctx context.Context, req *pb.AllRequest) (*pb.AllReply, error) {
	citys := []*pb.CityInfo{
		&pb.CityInfo{
			Id:     101,
			Code:   "bj",
			Name:   "北京",
			Status: 1,
		},
		&pb.CityInfo{
			Id:     102,
			Code:   "sh",
			Name:   "上海",
			Status: 1,
		},
	}

	len := len(citys)
	mapCity := make(map[string]*pb.CityInfo, len)
	hashCity := make(map[string]string, len)
	for _, city := range citys {
		mapCity[city.Code] = city
		hashCity[city.Code] = city.Name
	}

	res := &pb.AllReply{
		List: citys,
		Map:  mapCity,
		Hash: hashCity,
	}
	return res, nil
}
